package com.dd.cloud.common.annotate.validation.impl;

import com.dd.cloud.common.annotate.validation.Password;
import com.dd.cloud.common.constants.RegularConstants;
import com.dd.cloud.common.utils.RegexUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 密码验证实现类
 *
 * @author zk
 */
public class PasswordValidation implements ConstraintValidator<Password, Object> {
    private Password password;

    @Override
    public void initialize(Password passwordVal) {
        password = passwordVal;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        String ph = (String) value;
        if (!password.required() && ph == null) {
            return true;
        }
        if (password.required() && ph == null) {
            return false;
        }
        return RegexUtils.regCheck(ph, RegularConstants.PWD_REGEXP);
    }
}
